package com.jobcn.spider;

import com.jobcn.dic.SpiderDic;
import com.jobcn.entity.SpiderInfo;
import com.jobcn.entity.SpiderMer;
import com.jobcn.repository.spider.SpiderInfoRepository;
import com.jobcn.repository.spider.SpiderMerRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import us.codecraft.webmagic.ResultItems;
import us.codecraft.webmagic.Task;
import us.codecraft.webmagic.pipeline.Pipeline;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

/**
 * 处理爬取的数据,持久化到数据库
 * Created by winson on 17-6-20.
 */
@Component
public class Pipeline58 implements Pipeline {

    @Autowired
    private SpiderInfoRepository spiderInfoRepository;
    @Autowired
    private SpiderMerRepository spiderMerRepository;

    @Override
    public void process(ResultItems resultItems, Task task) {
        Map<String, Object> map = resultItems.getAll();

        if (map.get("taskType") == null) {
            return;
        }
        synchronized (this) {
            if ((Integer) map.get("taskType") == SpiderDic.SPIDER_TYPE_SER_INFO) { // 商家服务信息
                if (map.get("addressCode") == null)
                    return;
                //查找是否已经存在
                SpiderInfo spiderInfo = spiderInfoRepository.findByInfoId(map.get("infoId").toString());
                SpiderInfo obj = new SpiderInfo();
                obj.setTaskId(Integer.parseInt(map.get("taskId").toString()));
                obj.setInfoId(map.get("infoId").toString());
                if (map.get("infoDescription") != null)
                    obj.setInfoDescription(map.get("infoDescription").toString().replace("联系我时，请说是在58同城看到的，谢谢！", "").replace("展开更多描述 <>", ""));
                if (map.get("infoName") != null)
                    obj.setInfoName(map.get("infoName").toString());
                obj.setInfoSource(1);
                obj.setUpdateTime(new Date());
                if (map.get("linkName") != null)
                    obj.setLinkName(map.get("linkName").toString());
                String infoUpdatetime = map.get("infoUpdatetime").toString();
                infoUpdatetime = infoUpdatetime.substring(0, 10);
                try {
                    obj.setInfoUpdateTime(new SimpleDateFormat("yyyy-MM-dd").parse(infoUpdatetime));
                } catch (ParseException e) {
                    e.printStackTrace();
                }
                obj.setInfoPrice(map.get("infoPrice") != null ? map.get("infoPrice").toString() : null);
                if (map.get("infoClass") != null)
                    obj.setInfoClass(map.get("infoClass").toString());
                obj.setUrl(map.get("url").toString());
                if (map.get("merId") != null) {
                    String merId = map.get("merId").toString();
                    obj.setMerId(merId.substring(merId.indexOf("com") + 4, merId.length()));
                }
                if (map.get("serviceArea") != null)
                    obj.setServiceArea(map.get("serviceArea").toString());
                if (map.get("address") != null)
                    obj.setAddress(map.get("address").toString());
                obj.setAddressCode(map.get("addressCode").toString());

                if (spiderInfo != null) {
                    obj.setId(spiderInfo.getId());
                    obj.setSpiderTime(spiderInfo.getSpiderTime());
                } else {
                    obj.setSpiderTime(new Date());
                }
                try {
                    spiderInfoRepository.save(obj);
                } catch (Exception e) {

                }
            } else if ((Integer) map.get("taskType") == SpiderDic.SPIDER_TYPE_MER_INFO
                    || (Integer) map.get("taskType") == SpiderDic.SPIDER_TYPE_MER_MAP
                    || (Integer) map.get("taskType") == SpiderDic.SPIDER_TYPE_MER_CREDIT) {
                SpiderMer obj = spiderMerRepository.findByMerId(map.get("merId").toString());
                if (obj == null) {
                    obj = new SpiderMer();
                    obj.setMerId(map.get("merId").toString());
                    obj.setUpdateTime(new Date());
                    obj.setInsertTime(new Date());
                    obj.setMerSource(SpiderDic.SPIDER_SOURCE_58);
                }
                obj.setTaskId(Integer.parseInt(map.get("taskId").toString()));
                switch ((Integer) map.get("taskType")) {
                    case SpiderDic.SPIDER_TYPE_MER_INFO:    //服务信息
                        obj.setCertType(Integer.parseInt(map.get("certType").toString()));
                        if (map.get("merDescription") != null)
                            obj.setMerDescription(map.get("merDescription").toString());
                        obj.setMerName(map.get("merName").toString());
                        obj.setPhone(map.get("phone").toString());
                        obj.setUrl(map.get("url").toString());
                        obj.setAddress(map.get("address").toString());
                        break;
                    case SpiderDic.SPIDER_TYPE_MER_MAP: //商家地图坐标
                        obj.setLongitude(map.get("longitude").toString());
                        obj.setLatitude(map.get("latitude").toString());
                        break;
                    case SpiderDic.SPIDER_TYPE_MER_CREDIT:  //商家认证信息
                        obj.setCompanyName(map.get("companyName").toString());
                        if (map.get("creditCode") != null)
                            obj.setCreditCode(map.get("creditCode").toString());
                        obj.setRegisterDatetime((Date) map.get("registerDatetime"));
                        break;
                }
                spiderMerRepository.save(obj);
            }
        }
    }

}